package ${cfg.package_dir}.${cfg.cfgName};

import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 主类
* @author ${author}
* @since ${date}
*/
@Component
public class CORSFilterConf implements Filter {

    // 注入spring容器  请求和响应会触发
    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "*");
        //表示是否允许请求携带凭证信息,若要返回cookie、携带seesion等信息则将此项设置为true
        response.setHeader("Access-Control-Allow-Credentials", "true");
        chain.doFilter(req, res);
    }
    // 容器加载时触发
    @Override
    public void init(FilterConfig filterConfig) {

    }

    // 容器销毁时触发 如重启
    @Override
    public void destroy() {

    }
}
